package com.ysd.lis.service.cri.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysOrgDepartment;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.SysUserDepartment;
import com.ysd.lis.entity.cri.CriUserVsDept;
import com.ysd.lis.mapper.cri.CriUserVsDeptMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.cri.CriUserVsDeptService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-04-22
 */
@Service
public class CriUserVsDeptServiceImpl extends ServiceImpl<CriUserVsDeptMapper, CriUserVsDept> implements CriUserVsDeptService {

    @Autowired
    private CriUserVsDeptMapper criUserVsDeptMapper;
    @Override
    public Result findCriUserDeptList(CommonPageDto pageParam) {
        MPJLambdaWrapper<CriUserVsDept> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(CriUserVsDept.class)
                .selectAs("ls", SysOrgDepartment::getName,CriUserVsDept::getDeptName)
                .leftJoin(SysUser.class,"ld",SysUser::getUsername,CriUserVsDept::getUsername)
                .leftJoin(SysOrgDepartment.class,"ls",SysOrgDepartment::getId,CriUserVsDept::getDeptId);

        if(ToolsUtils.isNotEmpty(pageParam.getUsername())){
            queryWrapper.eq(CriUserVsDept::getUsername,pageParam.getUsername());
        }
        queryWrapper.eq(CriUserVsDept::getDelFlag,0)
                .orderByAsc(CriUserVsDept::getCreateTime);
        List<CriUserVsDept> itemList = criUserVsDeptMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", itemList);
    }

    @Override
    public Result batchDeleteUserVsDeptList(List<CriUserVsDept> criUserVsDepts) {
        List<String> ids = criUserVsDepts.stream().map(CriUserVsDept::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result batchAddUserDeptList(List<CriUserVsDept> criUserVsDepts) {
        boolean b = this.saveBatch(criUserVsDepts);
        if(b){
            return Result.succ(1, "添加成功", null);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }
}
